﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.BLL
{
    public partial class ProductBrandsService
    {
        public ProductBrands LoadByID(long id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }

        public ProductBrands LoadByName(string brandName)
        {
            return LoadEntities(a => a.BrandName == brandName).FirstOrDefault();
        }

        public IQueryable<ProductBrands> GetBrandsLikeName(string brandName)
        {
            return LoadEntities(a => a.BrandName.Contains(brandName));
        }
        public bool IsExist(string brandName, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ProductBrands>();
            if (!string.IsNullOrWhiteSpace(brandName))
                expressionWhere = expressionWhere.And(o => o.BrandName.Equals(brandName));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }
        public bool IsExistBrandCode(string brandCode, long id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ProductBrands>();
            if (!string.IsNullOrWhiteSpace(brandCode))
                expressionWhere = expressionWhere.And(o => o.BrandName.Equals(brandCode));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }

        /// <summary>
        /// 获得所有配件类型的品牌
        /// </summary>
        /// <returns></returns>
        public IQueryable<ProductBrands> GetAllProductBrand()
        {
            return LoadEntities(c => c.IsAvailable).OrderBy(o => o.Sort);
        }

        public IQueryable<ProductBrands> GetPagingList(ProductBrandsModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<ProductBrands>();
            if (model.ProductTypeId>0)
            {
                expressionWhere = expressionWhere.And(c => c.ProductTypeId == model.ProductTypeId);
            }
            if (model.IsAvailable.HasValue)
            {
                expressionWhere = expressionWhere.And(c => c.IsAvailable == model.IsAvailable);
            }
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere = expressionWhere.And(o => (o.BrandName).Contains(model.Keyword));
            }
            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "Sort asc";
            }
            return LoadPagerEntities<ProductBrands>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }


        #region 获取品牌名称 + string GetBrandName(long productBrandId)
        /// <summary>
        /// 获取品牌名称
        /// </summary>
        /// <param name="productBrandId">品牌ID</param>
        /// <returns></returns>
        public string GetBrandName(long productBrandId)
        {
            var item = LoadByID(productBrandId);
            if (item == null)
                return "";
            return item.BrandName;
        }
        #endregion
    }
}
